package com.deloitte.snap.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.web.context.ContextLoaderServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringContextLoaderServlet extends ContextLoaderServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7927463088021859585L;
	
	private static ServletContext currentServletContext = null; 
	private static  WebApplicationContext springWebApplicationContext = null;
	
	public void init()throws ServletException{
		super.init();
		
		if(currentServletContext == null){
			currentServletContext = getServletContext(); 
		}
	}
	public void init(ServletConfig config)throws ServletException{
		super.init(config);
		
		if(currentServletContext == null){
			currentServletContext = getServletContext(); 
		}
	}
	
	public static ServletContext getCurrentServletContext(){
		return currentServletContext;
	}
	
	public static WebApplicationContext getSpringWebApplicationContext(){
		if(springWebApplicationContext == null){
			springWebApplicationContext = WebApplicationContextUtils.getWebApplicationContext(currentServletContext);
		}
		return springWebApplicationContext;
	}
}
